package com.example.remotecontrol.ui.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.example.remotecontrol.data.model.Device
import com.example.remotecontrol.ui.remote.ACRemoteScreen
import com.example.remotecontrol.ui.remote.RemoteScreen
import com.example.remotecontrol.ui.remote.RemoteSelectionScreen

/**
 * 遥控器导航图
 * 管理遥控器相关页面之间的导航
 */
@Composable
fun RemoteNavGraph(
    navController: NavHostController = rememberNavController(),
    startDestination: String = RemoteDestinations.REMOTE_SELECTION
) {
    // 记住嵌套路由
    val actions = remember(navController) { RemoteNavigationActions(navController) }
    
    NavHost(
        navController = navController,
        startDestination = startDestination
    ) {
        // 遥控器选择页面
        composable(RemoteDestinations.REMOTE_SELECTION) {
            RemoteSelectionScreen(
                onNavigateToTVRemote = actions.navigateToTVRemote,
                onNavigateToACRemote = actions.navigateToACRemote,
                onNavigateToAudioRemote = actions.navigateToAudioRemote,
                onNavigateToProjectorRemote = actions.navigateToProjectorRemote,
                onNavigateToDeviceRemote = { device ->
                    actions.navigateToDeviceRemote(device.id)
                }
            )
        }
        
        // 电视遥控器页面
        composable(RemoteDestinations.TV_REMOTE) {
            RemoteScreen()
        }
        
        // 空调遥控器页面
        composable(RemoteDestinations.AC_REMOTE) {
            ACRemoteScreen()
        }
        
        // 音响遥控器页面 (待实现)
        composable(RemoteDestinations.AUDIO_REMOTE) {
            // AudioRemoteScreen() - 待实现
            RemoteScreen() // 临时使用通用遥控器
        }
        
        // 投影仪遥控器页面 (待实现)
        composable(RemoteDestinations.PROJECTOR_REMOTE) {
            // ProjectorRemoteScreen() - 待实现
            RemoteScreen() // 临时使用通用遥控器
        }
        
        // 特定设备遥控器页面
        composable(
            route = "${RemoteDestinations.DEVICE_REMOTE}/{deviceId}",
            arguments = listOf(
                navArgument("deviceId") { type = NavType.StringType }
            )
        ) { backStackEntry ->
            val deviceId = backStackEntry.arguments?.getString("deviceId") ?: ""
            // 这里可以根据设备类型导航到不同的遥控器
            RemoteScreen(deviceId = deviceId)
        }
    }
}

/**
 * 遥控器导航目的地
 */
object RemoteDestinations {
    const val REMOTE_SELECTION = "remote_selection"
    const val TV_REMOTE = "tv_remote"
    const val AC_REMOTE = "ac_remote"
    const val AUDIO_REMOTE = "audio_remote"
    const val PROJECTOR_REMOTE = "projector_remote"
    const val DEVICE_REMOTE = "device_remote"
}

/**
 * 遥控器导航动作
 */
class RemoteNavigationActions(private val navController: NavHostController) {
    
    val navigateToRemoteSelection: () -> Unit = {
        navController.navigate(RemoteDestinations.REMOTE_SELECTION) {
            popUpTo(RemoteDestinations.REMOTE_SELECTION) {
                saveState = true
            }
            launchSingleTop = true
            restoreState = true
        }
    }
    
    val navigateToTVRemote: () -> Unit = {
        navController.navigate(RemoteDestinations.TV_REMOTE)
    }
    
    val navigateToACRemote: () -> Unit = {
        navController.navigate(RemoteDestinations.AC_REMOTE)
    }
    
    val navigateToAudioRemote: () -> Unit = {
        navController.navigate(RemoteDestinations.AUDIO_REMOTE)
    }
    
    val navigateToProjectorRemote: () -> Unit = {
        navController.navigate(RemoteDestinations.PROJECTOR_REMOTE)
    }
    
    fun navigateToDeviceRemote(deviceId: String) {
        navController.navigate("${RemoteDestinations.DEVICE_REMOTE}/$deviceId")
    }
    
    val navigateUp: () -> Unit = {
        navController.navigateUp()
    }
} 